/**
 * 定义RemoteControlController远程控制
 */
(function(window, angular, undefined){

	var DataModificationController = angular.module('water.apps.voc.dataModification.modifyCertificateManager', ['ui.router','itfreer','waterCommon','RemoveControlService']);
		
	/**
	 * 修约凭证管理控制器
	 */
	DataModificationController.controller('ModifyCertificateMgrController',	['$scope','$itfreerHttp','$waterDataCommon','$ReportService','UploadService','DateService','OperationStationJobService','$NavService', function($scope,$itfreerHttp,$waterDataCommon,$ReportService,UploadService,DateService,OperationStationJobService,$NavService){
		
		$ReportService.setSelectedTab("modify.certificateMgr");
		//$NavService.setSelectedTab("DataMonitor.certificateMgr");
		//查询凭证
		var start = DateService.addDate(new Date(),-9);
		var end = DateService.addDate();
		$scope.queryObject = {startDate:start,endDate:end};
		$scope.queryCertificate = function(){
			if(!($waterDataCommon.judgmentDate($scope.queryObject.startDate,$scope.queryObject.endDate))){
				$.messager.alert("提示","结束日期不能小于开始日期");
				return;
			}
			var queryObj = {};
			queryObj.START = $scope.queryObject.startDate;
			queryObj.END = $scope.queryObject.endDate;
			$waterDataCommon.getPageRows("/DataModification/getCertificateDataList.do","CertificateDataList",queryObj,10,1,function(r){
				
			});
		};
		
		//新增凭证
		$scope.certificate_type = {list:[{name:'企业异常',type:'0'},{name:'企业停水',type:'2'},{name:'企业停产',type:'3'},{name:'设备故障',type:'4'},{name:'其他',type:'1'}],type:'0'};
		$scope.certObj = {CERTIFICATE_NAME:'',REMARK:''};
		$scope.addCertificate = function(){
			$scope.fileList = [];
			$scope.certObj = {CERTIFICATE_NAME:'',
					REMARK:'',
					USER_NAME:($scope.currentUser.S_USERID==-1?'超级管理员':$scope.currentUser.C_USEREALNAME),
					USER_ID:$scope.currentUser.S_USERID};
			$scope.certificate_type.type = '0';
			$itfreer.openWindow('AddCertificate',{width:800,height:250});
		};
		//查看/编辑凭证
		$scope.viewOrEditCertificate = function(curRow){
			$scope.fileList = [];
			queryCertificateFile(curRow.GUID,function(r){
				if(r && r.data){
					angular.forEach(r.data,function(value,key){
						$scope.fileList.push(value);
					});
					$scope.certObj.USER_ID = curRow.USER_ID;
					$scope.certObj.USER_NAME = curRow.USER_ID==-1?'超级管理员': curRow.C_USEREALNAME;
					$scope.certObj.CERTIFICATE_NAME = curRow.CERTIFICATE_NAME;
					$scope.certificate_type.type = curRow.CERTIFICATE_TYPE;
					$scope.certObj.REMARK = curRow.REMARK;
					$scope.certObj.GUID=curRow.GUID;
					$itfreer.openWindow('AddCertificate',{width:800,height:250});
				}
			});
		};
		
		//保存凭证
		$scope.saveCertificate = function(){
			if($scope.fileList.length>0){
			var saveObj = {};
			saveObj.CERTIFICATE_NAME = $scope.certObj.CERTIFICATE_NAME;
			saveObj.REMARK = $scope.certObj.REMARK;
			saveObj.CERTIFICATE_TYPE = $scope.certificate_type.type;
			saveObj.USER_ID = $scope.certObj.USER_ID;
			saveObj.CREATE_TIME = DateService.addDateTime();
			saveObj.GUID = $scope.certObj.GUID;
			OperationStationJobService.postQuery("/DataModification/saveCertificate.do",saveObj).then(function(d){
				if(d){
					var list = [];
					angular.forEach($scope.fileList,function(value,key){
						list.push(value.FILE_PATH);
					});
					if(list.length && d.GUID){
						saveObj.FILES = list.toString();
						saveObj.CERTIFICATE_ID = d.GUID;
						OperationStationJobService.postQuery("/DataModification/saveCertificateFileList.do",saveObj).then(function(r){
							$itfreer.closeWindow("AddCertificate");
							$.messager.alert("提示","保存成功");
							$scope.queryCertificate();
			            });
					}
					else{
						$itfreer.closeWindow("AddCertificate");
						$.messager.alert("提示","保存成功");
						$scope.queryCertificate();
					}
				}
			});
		   }
			else{
				$.messager.alert("提示","文件不能为空！");
			}
		};
		
		$scope.viewCertificate = function(id){
			$itfreer.openWindow('CertificateDetail',{width:800,height:250});
			$waterDataCommon.getPageRows("/DataModification/getCertificateInfo.do","CertificateInfo",{CERTIFICATE_ID:id},10,1,function(r){
				$scope.certificate = r;
			});
		}
		
		//删除凭证文件
		$scope.deleteCertificateFile = function(curRow){
			$.messager.confirm("确认","确定要删除吗？",function(r){
				if(r){
					$itfreerHttp.deleteByKey("MONITOR_DATA_CERTIFICATE_FILE", {GUID:curRow.GUID},function(){
						queryCertificateFile(curRow.CERTIFICATE_ID,function(r){
							if(r && r.data){
								$scope.fileList=[];
								angular.forEach(r.data,function(value,key){
									$scope.fileList.push(value);
								});
								$scope.queryCertificate();
							}
						});
					})
				}
			});
		};
		//下载凭证文件
		$scope.downloadFile = function(curRow){
			var fileInfo = curRow.FILE_PATH;
			if (!fileInfo) return;
			fileInfoArr = fileInfo.split(":");
			var url = fileInfoArr[0];
			var name = fileInfoArr[1];
			var downloadUrl = getBasePath()+'/plupload/download.do';
			var _downloadUrl = downloadUrl+"?filePath="+url.replace(getBasePath()+"/","")+"&fileName="+encodeURIComponent(name);
			window.open(_downloadUrl);
		};
		//删除凭证
		$scope.deleteCertificate = function(curRow){
			var obj = {'CERTIFICATE_ID':curRow.GUID};
			var msg = '';
			OperationStationJobService.postQuery("/DataModification/getModifyDataCountByCertificate.do",obj).then(function(r){
				if(r){
					if(r.rowcount==0){
						msg = '确定要删除记录吗？';
					}else{
						msg = '当前凭证已经关联了修约数据，确定要删除吗？';
					}
					$.messager.confirm('确认',msg,function(yes){
						if(yes){
							OperationStationJobService.postQuery("/DataModification/deleteCertificate.do",obj)
							.then(function(r){
								if(r && r.data=='success'){
									$.messager.alert("提示","删除成功！");
								}else{
									$.messager.alert("提示","删除失败！");
								}
								$scope.queryCertificate();
							});
						}
					});
				}
            });
		};
		
		//上传凭证文件
		$scope.fileList = [];
		function uploadSuccessHandler(fileInfo){
			$scope.fileList.push({"FILE_PATH":fileInfo.file});
			$scope.$apply();
		}
		//查询凭证文件
		function queryCertificateFile(certificate_id,callback){
			var queryObj = {};
			queryObj.CERTIFICATE_ID = certificate_id;
			$waterDataCommon.getPageRows("/DataModification/getCertificateFileList.do","CertificateFileList",queryObj,10,1,callback);
		}
		$scope.uploadCertificateFile = function(){
			UploadService.upload(uploadSuccessHandler);
		};
		
		$scope.closeCertWindow = function(){
			$itfreer.closeWindow("AddCertificate");
		}
		
		$scope.queryCertificate();
		$scope.waterDataCommon = $waterDataCommon;
	}]);	
	
}(window, angular));